import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedList;


public class FiveDMatrix extends Matrix{

	public FiveDMatrix()
	{
		matrix = new LinkedList<ValueSlot>();
		data = new LinkedList<Long>();
	}
	
	@Override
	public void process() {
		//long count = 0;
		int size = data.size();
		for(int i=0; i < size; i++)
		{
			for(int j=0; j<size; j++)
			{
				if(j!=i)
				{
					for(int k=0; k<size; k++)
					{
						if(k!= j && k!=i)
						{
							for(int l=0; l<size; l++)
							{
								if(l!=k && l!=j && l!=i)
								{
									for(int m=0; m<size; m++)
									{
										if(m!=l && m!=k && m!=j && m!=i)
										{
											/*long a = data.get(i);
											long b = data.get(j);
											long c = data.get(k);
											long d = data.get(l);
											long e = data.get(m);*/
											//if((data.get(j) - data.get(k) > 0) && (data.get(l) - data.get(m) > 0) && (data.get(k) > data.get(l)))
											//{
												long val;
												val = data.get(i) + data.get(j) - data.get(k) + data.get(l) - data.get(m);
												insert(val);
											//}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	public void insert(long val)
	{
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTimeInMillis(val);
		boolean exist = false;
		for(ValueSlot vs: matrix)
		{
			if(match(cal, vs.getDate()))
			{
				exist = true;
				vs.insert();
			}
		}
		
		if(!exist)
		{
			ValueSlot vs = new ValueSlot(val);
			matrix.add(vs);
		}
	}
	
	
	public int getAverageOccurance()
	{
		int temp = 0;
		for(ValueSlot vs: matrix)
		{
			temp = temp + vs.getOccurance();
		}
		
		if(matrix.size()!=0)
		{
			return temp / matrix.size();
		}
		else {
			return 0;
		}
	}
	
	/*
	 * generalise all occurances by dividing them by average
	 */
	public void generalise()
	{
		int average = getAverageOccurance();
		for(ValueSlot vs:matrix)
		{
			vs.setOccurance(vs.getOccurance() * 100);
			vs.setOccurance(vs.getOccurance() / average);
		}
	}
	
	public void eliminateAverage()
	{
		int average = getAverageOccurance();
		for(int i=0; i < matrix.size(); i++)
		{
			ValueSlot vs = matrix.get(i);
			if(vs.getOccurance() < average*100)
			{
				matrix.remove(i);
			}
		}
	}
	
	public double getPercentageforDay(GregorianCalendar date)
	{
		ValueSlot target = null;
		int average = getAverageOccurance();
		for(ValueSlot vs:matrix)
		{
			if(match(vs.getDate(), date))
			{
				target = vs;
			}
		}
		
		if(target == null)
		{
			return -1;
		}
		else
		{
			if(target.getOccurance() == 0)
			{
				return -2;
			}
			else
			{
				System.out.println("average is: " + average + ", and occurance for: " + target.getDate().getTime() + " is: " + target.getOccurance());
				double result = (double)average / (double)target.getOccurance();
				System.out.println("And percentage is: " + result);
				return result;
			}
		}
	}
	
	public String getPercentageString(GregorianCalendar date)
	{
		ValueSlot target = null;
		int average = getAverageOccurance();
		for(ValueSlot vs:matrix)
		{
			if(match(vs.getDate(), date))
			{
				target = vs;
			}
		}
		
		if(target == null)
		{
			return "non found";
		}
		else
		{
			if(target.getOccurance() == 0)
			{
				return "Occurance is 0";
			}
			else
			{
				double result = (double)average / (double)target.getOccurance();
				return "5D: percentage: " + result + "\n";
			}
		}
	}

}
